package com.kgd.news.ibeetl.tag;

import com.kgd.news.entity.SysMenu;
import org.beetl.core.Tag;

import java.util.List;
import java.util.Map;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/7/31
 * @Time: 下午10:28
 */
public class MenuTag extends Tag {

    @Override
    public void render() {
        try {
            Map attrs = (Map) args[1];
            Integer selVal = Integer.valueOf(attrs.get("val") == null ? "-1" : (attrs.get("val") + ""));
            String pname = attrs.get("pname") + "";
            String defaultNumber = attrs.get("defaultNumber") + "";
            ctx.byteWriter.writeString(select(selVal, pname, defaultNumber));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }


    /**
     * 下拉标签
     *
     * @param selVal
     * @param defaultnumber
     * @return
     */
    private String select(int selVal, String pname, String defaultnumber) {
        StringBuilder sb = new StringBuilder();
        /*if (StrKit.notBlank(id)) {
            sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
            sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
        } else {*/
        sb.append("<select name=\"").append(pname).append("\" >");
        // }
        sb.append("<option value=\"").append("").append("\">");
        sb.append("请选择");
        sb.append("</option>");

        SysMenu menu = SysMenu.dao.findFirst("select * from sys_menu where id = ?", selVal);
        List<SysMenu> menuList = SysMenu.dao.find("select * from sys_menu where parentid is null");

        for (SysMenu r : menuList) {
            String name = r.getMName();
            Long id = r.getId();
            if ((null != menu && id.equals(menu.getParentid())) || id.equals(defaultnumber)) {// 默认选中
                sb.append("<option value=\"").append(id).append("\" selected=\"selected\">");
                sb.append(name);
                sb.append("</option>");
            } else {
                sb.append("<option value=\"").append(id).append("\">");
                sb.append(name);
                sb.append("</option>");
            }
        }
        sb.append("</select>");

        return sb.toString();
    }
}
